<?php

namespace App\Http\Controllers\Api\Admin;

use App\Http\Services\AdminManager;
use App\Http\Services\CommonManager;
use App\IntegralDetail;
use App\OrderItem;
use App\Orders;
use App\Staff;
use App\User;
use App\UserInfo;
use App\VipInfo;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class StaffController extends Controller
{
    /**
     * 获取人员列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getStaffList(Request $request){
        /** @var User $adminUser */
        $adminUser = AdminManager::isLogin($request);
        if(!$adminUser)
            return response()->json(['level' => 'error','message' => '请先登录！','data' => 'no_login']);

        $limit     = $request->input('limit',18);
        $page      = $request->input('page',1);
        $keyword   = $request->input('keyword','');
        $startTime = $request->input('start_time','');
        $endTime   = $request->input('end_time','');

        $pageInfo = [
            'limit' => $limit,
            'page' => $page
        ];

        $page -= 1;

        /** @var Staff $staff */
        $staffList = Staff::where('id' , '>' , 0);

        if($keyword != ''){
            $staffList->where('realname','like','%'.trim($keyword).'%')->orWhere('phone','like','%'.trim($keyword).'%');
        }

        if($startTime != '' && $endTime != ''){
            $staffList = $staffList->whereBetween('created_at',[$startTime,$endTime]);
        }

        $pageInfo['total'] = $staffList->count();

        $staffList = $staffList->orderBy('id','desc')->offset($limit * $page)->limit($limit)->get();
        if(count($staffList) > 0){
            foreach ($staffList as $staff) {
                $staff->user_info = CommonManager::getUserBasicInfo($staff->user_id);
            }
        }

        return response()->json(['level' => 'success','message' => '获取成功','data' => $staffList,'page_info' => $pageInfo]);
    }

    /**
     * 编辑人员
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function setStaff(Request $request){
        /** @var User $adminUser */
        $adminUser = AdminManager::isLogin($request);
        if(!$adminUser)
            return response()->json(['level' => 'error','message' => '请先登录！','data' => 'no_login']);

        $realname = $request->input('realname','');
        $phone    = $request->input('phone','');
        $userId   = (int)$request->input('user_id',0);
        $id       = $request->input('id',0);

        $staff = Staff::whereId($id)->first();
        if(!$staff)
            $staff = new Staff();

        $staff->user_id  = $userId;
        $staff->realname = $realname;
        $staff->phone    = $phone;
        $staff->save();

        if(empty($staff->staff_key)){
            $staff->staff_key = md5('staff' . $staff->id . time() . rand(1000,9999));
            $staff->save();
        }

        return response()->json(['level' => 'success','message' => '设置成功','data' => $staff]);
    }

    /**
     * 删除人员
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \Exception
     */
    public function delStaff(Request $request){
        /** @var User $adminUser */
        $adminUser = AdminManager::isLogin($request);
        if(!$adminUser)
            return response()->json(['level' => 'error','message' => '请先登录！','data' => 'no_login']);

        $id = $request->input('id',0);

        $staff = Staff::whereId($id)->first();
        if(!$staff)
            return response()->json(['level' => 'error','message' => '人员不存在']);

        $staff->delete();

        return response()->json(['level' => 'success','message' => '删除成功','data' => $staff]);
    }

    /**
     * 邀请注册列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function inviteRegisterList(Request $request){
        /** @var User $adminUser */
        $adminUser = AdminManager::isLogin($request);
        if(!$adminUser)
            return response()->json(['level' => 'error','message' => '请先登录！','data' => 'no_login']);

        $userId = $request->input('user_id',0);
        $limit  = $request->input('limit',16);
        $page   = $request->input('page',1);

        $pageInfo=[
            'page'=>$page,
            'limit'=>$limit
        ];
        $page=$page-1;


        $users = User::whereInviteUserId($userId);

        $users = $users->whereRole(0);

        $pageInfo['total']=$users->count();

        $users = $users->orderByDesc('id')
            ->offset($limit * $page)
            ->limit($limit)
            ->get();

        if (count($users) > 0){
            /** @var User $user */
            foreach ($users as $user){
                $user->invite_user_info = UserInfo::getUserInfo($user->invite_user_id);
                $user->user_info=UserInfo::getUserInfo($user->id);
                $user->vip_info=VipInfo::getUserVipInfo($user->id);
            }
        }

        return response()->json(['level'=>'success','message'=>'success','data'=>$users,'page_info'=>$pageInfo]);
    }

    /**
     * 邀请购买列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function inviteBuyList(Request $request){
        /** @var User $adminUser */
        $adminUser = AdminManager::isLogin($request);
        if(!$adminUser)
            return response()->json(['level' => 'error','message' => '请先登录！','data' => 'no_login']);

        $userId = $request->input('user_id',0);
        $limit  = $request->input('limit',16);
        $page   = $request->input('page',1);

        $pageInfo=[
            'page'=>$page,
            'limit'=>$limit
        ];
        $page=$page-1;

        /** @var Orders $data */
        $data       = Orders::whereInviteUserId($userId);

        $pageInfo['total']=$data->count();
        $data = $data->orderByDesc('id')
            ->offset($page * $limit)
            ->limit($limit)
            ->get();
        if (count($data) > 0){
            /** @var Orders $item */
            foreach ($data as $item){
                $item->invite_user_info = UserInfo::getUserInfo($item->invite_user_id);
                $item->user_info    = UserInfo::getUserInfo($item->user_id);
                $item->vip_type_name = VipInfo::getVipTypeName($item->vip_type);
                $item->order_item   = OrderItem::getOrderItem($item->id);
                $item->pay_way_name = '';
                if($item->package_id != 0){
                    $item->type_name = '套餐订单';
                }elseif($item->group_id != 0){
                    $item->type_name = '拼团订单';
                }else{
                    $item->type_name = '常规订单';
                }

                if($item->pay_way == 'wechatpay'){
                    $item->pay_way_name = '微信支付';
                }elseif($item->pay_way == 'alipay'){
                    $item->pay_way_name = '支付宝支付';
                }elseif($item->pay_way == 'integralpay'){
                    $item->pay_way_name = '积分支付';
                }

                $item->get_integral = 0;
                $getIntegralDetail = IntegralDetail::whereUserId($item->invite_user_id)->whereOrderNum($item->order_num)->first();
                if($getIntegralDetail){
                    $item->get_integral = $getIntegralDetail->integral_num;
                }
            }
        }
        return response()->json([
            'level'     => 'success',
            'message'   => 'success',
            'data'      => $data,
            'page_info' => $pageInfo
        ]);
    }
}
